﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using MvcBJDCWebApp.DAL;
using MvcBJDCWebApp.Models.League;

namespace MvcBJDCWebApp.Controllers
{
    public class LeagueDetailsController : Controller
    {
        private LeagueContext db = new LeagueContext();

        // GET: LeagueDetails
        public ActionResult Index()
        {
            return View(db.LeagueDetails.ToList());
        }

        public ActionResult Show(int? leagueTypeID, string leagueKey)
        {
            if (!string.IsNullOrEmpty(leagueKey))
            {
                var league = db.Leagues.FirstOrDefault(c => c.LeagueKey == leagueKey);

                var leagueType = db.LeagueTypes.FirstOrDefault(l => l.LeagueID == league.LeagueID);

                return View(db.LeagueDetails.Where(c => c.LeagueTypeID == leagueType.LeagueTypeID).ToList());

                //  return MsgList.FirstOrDefault(x =>
                //x.MD_TABLE.Equals("tableset") &&
                //x.MD_FIELD.Equals("ID") &&
                //x.MD_VALUE.Equals(_id.ToString())) ?? new MSG_DESC();

                //if (league.Count==1)
                //{
                //    var leagueType = db.LeagueTypes.Where(l => l.LeagueID == 8).ToList();

                //    return View(db.LeagueDetails.Where(c => c.LeagueTypeID == leagueType[0].LeagueTypeID).ToList());
                //}
            }

            if (leagueTypeID != null && leagueTypeID > 0)
            {
                //get the detail 

                return View(db.LeagueDetails.Where(c => c.LeagueTypeID == leagueTypeID).ToList());
            }

            return View(db.LeagueDetails.ToList());

        }

        // GET: LeagueDetails/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            LeagueDetail leagueDetail = db.LeagueDetails.Find(id);
            if (leagueDetail == null)
            {
                return HttpNotFound();
            }
            return View(leagueDetail);
        }

        // GET: LeagueDetails/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: LeagueDetails/Create
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "LeagueDetailID,LeagueTypeID,c1,c2,c3,c4T1,c4T1URL,c4R,c4T2,c4T2URL,c51,c51Link,c52,c52Link,liveid")] LeagueDetail leagueDetail)
        {
            if (ModelState.IsValid)
            {
                db.LeagueDetails.Add(leagueDetail);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(leagueDetail);
        }

        // GET: LeagueDetails/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            LeagueDetail leagueDetail = db.LeagueDetails.Find(id);
            if (leagueDetail == null)
            {
                return HttpNotFound();
            }
            return View(leagueDetail);
        }

        // POST: LeagueDetails/Edit/5
        // 为了防止“过多发布”攻击，请启用要绑定到的特定属性，有关 
        // 详细信息，请参阅 https://go.microsoft.com/fwlink/?LinkId=317598。
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "LeagueDetailID,LeagueTypeID,c1,c2,c3,c4T1,c4T1URL,c4R,c4T2,c4T2URL,c51,c51Link,c52,c52Link,liveid")] LeagueDetail leagueDetail)
        {
            if (ModelState.IsValid)
            {
                db.Entry(leagueDetail).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(leagueDetail);
        }

        // GET: LeagueDetails/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            LeagueDetail leagueDetail = db.LeagueDetails.Find(id);
            if (leagueDetail == null)
            {
                return HttpNotFound();
            }
            return View(leagueDetail);
        }

        // POST: LeagueDetails/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            LeagueDetail leagueDetail = db.LeagueDetails.Find(id);
            db.LeagueDetails.Remove(leagueDetail);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
